#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
int n, l;
int c[N];

bool check(int mid)
{
    int num = 0;
    // int q = mid - 1;
    int r = 1;
    while (r <= n)
    {
        if (c[r] >= mid)
            ++r;
        else if (c[r] == mid - 1)
        {
            if (num < l)
                ++r, ++num;
            else
                break;
        }
        else
            break;
    }
    return (r - 1) >= mid;
}

int main()
{
    cin >> n >> l;
    for (int i = 1; i <= n; ++i)
        cin >> c[i];
    sort(c + 1, c + n + 1, greater<int>());
    int l = 0, r = n;
    // cout << r << endl;
    while (l < r)
    {
        int mid = (l + r + 1) >> 1;
        if (check(mid))
            l = mid;
        else
            r = mid - 1;
    }
    cout << l << endl;
    return 0;
}